package ${basePackage}.service;
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import java.util.Date;
</#if>

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import org.springframework.amqp.rabbit.core.RabbitTemplate;
</#if>
import com.unswift.annotation.api.Api;
import com.unswift.annotation.api.ApiField;
import com.unswift.annotation.api.ApiMethod;
import ${basePackage}.adapter.${module}.${entityName}Adapter;
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.adapter.logger.LoggerExportTaskAdapter;
</#if>
<#if stringUtils.contains(dataOperator,'create',',') || stringUtils.contains(dataOperator,'update',',') || stringUtils.contains(dataOperator,'delete',',')>
import ${basePackage}.annotation.logger.OperatorLogger;
import ${basePackage}.enums.OperatorTypeEnum;
</#if>
<#if stringUtils.contains(dataOperator,'create',',')>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}CreateBo;
</#if>
<#if stringUtils.contains(dataOperator,'delete',',')>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}DeleteBo;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}ExportBo;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}ExportStopBo;
</#if>
<#if stringUtils.contains(dataOperator,'list',',')>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}PageBo;
</#if>
<#if stringUtils.contains(dataOperator,'update',',')>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}UpdateBo;
</#if>
<#if stringUtils.contains(dataOperator,'view',',')>
import ${basePackage}.pojo.bo.${module}.${currModule}.${entityName}ViewBo;
</#if>
<#if stringUtils.contains(dataOperator,'list',',') || stringUtils.contains(dataOperator,'view',',')>
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}DataDo;
</#if>
<#if stringUtils.contains(dataOperator,'delete',',')>
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}DeleteDo;
</#if>
<#if stringUtils.contains(dataOperator,'create',',')>
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}InsertDo;
</#if>
<#if stringUtils.contains(dataOperator,'list',',')>
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}PageDo;
</#if>
<#if stringUtils.contains(dataOperator,'view',',')>
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}SingleDo;
</#if>
<#if stringUtils.contains(dataOperator,'update',',')>
import ${basePackage}.pojo.dao.${module}.${currModule}.${entityName}UpdateDo;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.pojo.dao.logger.export.LoggerExportTaskInsertDo;
</#if>
<#if stringUtils.contains(dataOperator,'list',',')>
import ${basePackage}.pojo.vo.page.PageVo;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.pojo.vo.logger.export.task.LoggerExportTaskQueueVo;
</#if>
<#if stringUtils.contains(dataOperator,'create',',')>
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}CreateVo;
</#if>
<#if stringUtils.contains(dataOperator,'delete',',')>
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}DeleteVo;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}ExportVo;
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}ExportStopVo;
</#if>
<#if stringUtils.contains(dataOperator,'list',',')>
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}PageVo;
</#if>
<#if stringUtils.contains(dataOperator,'update',',')>
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}UpdateVo;
</#if>
<#if stringUtils.contains(dataOperator,'view',',')>
import ${basePackage}.pojo.vo.${module}.${currModule}.${entityName}ViewVo;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import ${basePackage}.rabbit.RabbitConstant;
</#if>
<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
import com.unswift.utils.JsonUtils;
</#if>
<#if stringUtils.contains(dataOperator,'create',',') || stringUtils.contains(dataOperator,'update',',') || stringUtils.contains(dataOperator,'delete',',')>
import ${basePackage}.utils.LoggerUtils;
</#if>
import ${basePackage}.sql.${module}.${currModule}.${entityName}Sql;
<#if stringUtils.contains(dataOperator,'delete',',')>
import com.unswift.utils.ExceptionUtils;
</#if>

@Service
@Api(value="${tableComment!''}服务", author="${username}", date="${date}", version="1.0.0")
public class ${entityName}Service extends BaseService<#if stringUtils.contains(dataOperator,'export',',') && exportSame> implements RabbitConstant</#if>{
	
	@Autowired
	@ApiField("${tableComment!''}公共服务")
	private ${entityName}Adapter ${entitySampleName}Adapter;
	
	@Autowired
	@ApiField("${tableComment!''}自定义Sql")
	private ${entityName}Sql ${entitySampleName}Sql;
	<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
	
	@Autowired
	@ApiField("导出任务服务")
	private LoggerExportTaskAdapter loggerExportTaskAdapter;
	
	@Autowired
	@ApiField("Rabbit Mq发送消息服务")
	private RabbitTemplate rabbitTemplate;
	</#if>
	<#if stringUtils.contains(dataOperator,'list',',')>
	
	@ApiMethod(value="查询${tableComment!''}分页数据", params=@ApiField("分页对象，包含分页信息及查询条件"), returns=@ApiField("包含查询数据的分页对象"))
	public PageVo<${entityName}PageVo> findPageList(${entityName}PageBo searchBo){
		${entityName}PageDo search=this.convertPojo(searchBo, ${entityName}PageDo.class);//将Bo转换为Do
		search.setFirstSize((search.getCurrPage()-1)*search.getPageSize());
		PageVo<${entityName}DataDo> page=${entitySampleName}Adapter.findPageList(search);//将Do转换为Vo
		return this.convertPage(page, ${entityName}PageVo.class);
	}
	</#if>
	<#if stringUtils.contains(dataOperator,'view',',')>
	
	@ApiMethod(value="查询${tableComment!''}详情", params=@ApiField("详情业务实体，包含id字段"), returns=@ApiField("${tableComment!''}详情数据"))
	public ${entityName}ViewVo view(${entityName}ViewBo viewBo){
		${entityName}SingleDo search=this.convertPojo(viewBo, ${entityName}SingleDo.class);
		${entityName}DataDo single=${entitySampleName}Adapter.findSingle(search);
		return this.convertPojo(single, ${entityName}ViewVo.class);//将Do转换为Vo
	}
	</#if>
	<#if stringUtils.contains(dataOperator,'create',',')>

	@OperatorLogger(type = OperatorTypeEnum.CREATE)
	@ApiMethod(value="创建${tableComment!''}", params=@ApiField("创建的业务实体"), returns=@ApiField("创建结果{0：未创建，1：已创建}"))
	public ${entityName}CreateVo create(${entityName}CreateBo createBo){
		LoggerUtils.setModule(${entitySampleName}Adapter.getModule());//设置日志所属模块
		${entityName}InsertDo insert=this.convertPojo(createBo, ${entityName}InsertDo.class);//将Bo转换为Do
		int result=${entitySampleName}Adapter.save(insert, true);
		LoggerUtils.setId(insert.getId());//创建日志需要设置数据id
		return new ${entityName}CreateVo(result);
	}
	</#if>
	<#if stringUtils.contains(dataOperator,'update',',')>
	
	@OperatorLogger(type = OperatorTypeEnum.UPDATE)
	@ApiMethod(value="更新${tableComment!''}", params=@ApiField("更新的业务实体"), returns=@ApiField("更新结果{0：未创建，1：已创建}"))
	public ${entityName}UpdateVo update(${entityName}UpdateBo updateBo){
		LoggerUtils.setModule(${entitySampleName}Adapter.getModule());//设置日志所属模块
		${entityName}DataDo view=${entitySampleName}Adapter.findById(updateBo.getId());
		LoggerUtils.setOriginalData(view);//设置日志原数据对象
		${entityName}UpdateDo update=this.convertPojo(updateBo, ${entityName}UpdateDo.class);//将Bo转换为Do
		int result=${entitySampleName}Adapter.update(update, true);
		return new ${entityName}UpdateVo(result);
	}
	</#if>
	<#if stringUtils.contains(dataOperator,'delete',',')>
	
	@OperatorLogger(type = OperatorTypeEnum.DELETE)
	@ApiMethod(value="删除${tableComment!''}", params=@ApiField("删除业务实体，包含id字段"), returns=@ApiField("删除结果{0：未创建，1：已创建}"))
	public ${entityName}DeleteVo delete(${entityName}DeleteBo deleteBo){
		LoggerUtils.setModule(${entitySampleName}Adapter.getModule());//设置日志所属模块
		${entityName}DataDo deleteData=${entitySampleName}Adapter.findById(deleteBo.getId());
		ExceptionUtils.empty(deleteData, "delete.object.not.exists", "${tableComment!''}信息");//删除对象必须存在
		${entityName}DeleteDo delete=this.convertPojo(deleteBo, ${entityName}DeleteDo.class);//将Bo转换为Do
		int result=${entitySampleName}Adapter.delete(delete);
		return new ${entityName}DeleteVo(result);
	}
	</#if>
	<#if stringUtils.contains(dataOperator,'export',',') && exportSame>
	
	@ApiMethod(value="根据条件导出${tableComment!''}数据", params=@ApiField("查询条件"))
	public ${entityName}ExportVo export(${entityName}ExportBo exportBo){
		Date date=new Date();
		LoggerExportTaskInsertDo task=new LoggerExportTaskInsertDo();
		task.setName("${tableComment!''}导出");
		task.setDocumentType("excel");
		task.setStatus((byte)0);
		task.setStartTime(date);
		task.setExportBeanClass("${basePackage}.service.${entityName}ExportService");
		task.setExportModelClass("${basePackage}.pojo.bo.${module}.${currModule}.${entityName}ExportBo");
		task.setExportCondition(JsonUtils.toJson(exportBo));
		task.setResult((byte)0);
		task.setIsDelete(0);
		int result=loggerExportTaskAdapter.save(task, false);//保存任务，不用加事务，防止队列消费的时候还没保存完数据
		rabbitTemplate.convertAndSend(EXPORT_EXCHANGE_NAME, EXPORT_ROUTE_KEY, new LoggerExportTaskQueueVo(task.getId(), ExportTaskEventEnum.START));//发送导出队列
		return new ${entityName}ExportVo(task.getId(), result);
	}
	
	@ApiMethod(value="停止导出${tableComment!''}", params=@ApiField("停止导出对象"))
	public ${entityName}ExportStopVo stop(${entityName}ExportStopBo exportStopBo){
		rabbitTemplate.convertAndSend(EXPORT_STOP_EXCHANGE_NAME, EXPORT_STOP_ROUTE_KEY, new LoggerExportTaskQueueVo(exportStopBo.getId(), ExportTaskEventEnum.STOP));//发送停止导出队列
		return new ${entityName}ExportStopVo(1);
	}
	</#if>
}